* show csr_20 vs. growth in avplot with country labels

set scheme s2color
cd "~/Documents/econ/research/current/cognitive/estimation/dofiles"

// produce table with Barro growth regressions showing e0 vs. asr&csr //

use ../data/growth_panel, clear
xtset isonum year, delta(5)

label var lgdppw "\(ln(y_0)\)"
label var PRI "Institutions (0-100)"
label var trade_open "Trade openness (\% of GDP)"
label var e0 "Life expectancy (years)"
label var yr_sch "Schooling (years)"
label var csr "Child survival (per 1000)"
label var asr "Adult survival (per 1000)"
label var csr_20 "Child survival, previous generation"

tab year if year>=1985, gen(yd) // create year dummies after 1985
drop yd1

local av "csr_20"
local lagvars "lgdppw trade_open PRI yr_sch"
local gvars "gPWT5 `lagvars' yd*"
local gmmlag 3
local gmm_rhs "gmm(`lagvars', laglimits(2 `gmmlag'))"
local xtabopts "twostep robust"
local outopts "se varlabel squarebrack drop(yd*) noautosumm"


// asr & csr_20
di "xtabond2 `gvars' asr csr_20, `gmm_rhs' `xtabopts' gmm(asr, laglimits(2 `gmmlag'))"
xtabond2 `gvars' asr csr_20, `gmm_rhs' `xtabopts'	///
	gmm(asr, laglimits(2 `gmmlag')) svmat
sum csr_20 if e(sample)
scalar xmean = r(mean)
local xmin = 856
local xmax = 940
sum gPWT5 if e(sample)
scalar ymean = r(mean)

local ms = "*0.4 " * 2
local mls = "*1.1 " * 2
local cc = "countrycode " * 2
local clr = "navy maroon"
// make a black & white graph for the gavplot paper &
// generate ex & ey values for cogdev slides
xtavplot csr_20, gen(ex ey) gen2(ex2 ey2) addmean debug
scalar b = r(coef)
scalar se_b = r(se)

replace ex = ex + xmean
replace ex2 = ex2 + xmean
replace ey = ey + ymean
replace ey2 = ey2 + ymean
sum ex ex2 ey ey2	

gen ey_hic = ey if year==2015 & region=="HIC"
gen hic_incl = (ey_hic>=1.7 | ex>=910 | ey_hic<=1.26 | ex<=899)
gen ey_h3 = ey_hic if hic_incl & countrycode!="DNK" ///
			 & countrycode!="SVK" & countrycode!="GBR"
replace ey_hic = ey_hic if !hic_incl
gen ey_lmc = ey if year==2015 & region!="HIC"
gen lmc_incl = (ey_lmc>=1.7 | ex>=910 | ey_lmc<=1.26 | ex<=899)
gen l3_drop = (countrycode=="BGD" | countrycode=="COD" | countrycode=="DOM" | ///
		countrycode=="NAM" | countrycode=="MEX" | countrycode=="SEN" | 		///
		countrycode=="ZMB")
gen ey_l3 = ey_lmc if lmc_incl & !l3_drop
replace ey_lmc = ey_lmc if !lmc_incl
gen pos = 3
replace pos = 4  if countrycode=="ALB"
replace pos = 9  if countrycode=="ARM"
replace pos = 12 if countrycode=="BEL"
replace pos = 10 if countrycode=="BHR"
replace pos = 10 if countrycode=="CIV"
replace pos = 7  if countrycode=="COD"
replace pos = 9  if countrycode=="EST" 
replace pos = 2  if countrycode=="GBR"
replace pos = 3  if countrycode=="HND"
replace pos = 4  if countrycode=="IRN"
replace pos = 6  if countrycode=="ITA"
replace pos = 10 if countrycode=="JPN"
replace pos = 9  if countrycode=="LTU"
replace pos = 12 if countrycode=="LVA"
replace pos = 11 if countrycode=="MAR"
replace pos = 9  if countrycode=="MLI"
replace pos = 9  if countrycode=="MMR"
replace pos = 9  if countrycode=="PAK"
replace pos = 4  if countrycode=="PER"
replace pos = 12 if countrycode=="PHL"
replace pos = 12 if countrycode=="ROU"
replace pos = 10 if countrycode=="RUS"
replace pos = 8  if countrycode=="SLV"
replace pos = 6  if countrycode=="TUR"
replace pos = 9  if countrycode=="USA"

di "xmin = `xmin'"
di "xmax = `xmax'"

scalar list b
twoway function y = b*(x-xmean) + ymean, range(`xmin' `xmax') lcolor(dkgreen) lpattern(dash) lwidth(medthick) ///
	|| scatter ey ex if ex<`xmax' & ey<2.5 & ey>0.2 & ex>`xmin', msize(*0.2) mcolor(gs11) /// 
	|| scatter ey2 ex2 if ex2<`xmax' & ey2<2.5 & ey2>0.2 & ex2>`xmin', msize(*0.2) mcolor(gs11)	/// 
	|| scatter ey_hic ey_lmc ex if ex<`xmax' & ex>`xmin', mcolor(`clr') msize(`ms')	///
	|| scatter ey_h3 ey_l3 ex if ex<`xmax' & ex>`xmin', mcolor(`clr') msize(`ms') 	///
		mlabcol(`clr') mlabel(`cc') mlabsize(`mls')	mlabvp(pos) ///
	||, /*title(Economic growth 1985-90 versus child survival) 	///
		subtitle(other years in gs11) */	///
		ylabel(0.5(0.5)2.5, angle(0)) xlabel(860(10)930)		///
		ytitle("e(GDP per worker growth, 2015-19 | X)") 		///
		xtitle("e(Child survival rate 1980-1995 | X)") legend(off) ///
		text(-0.95 12 " ", box placement(right) width(32) height(12)	///
			alignment(top) fcolor(none))	///
		text(-0.85 13 "{&bull} High Income", color(navy)  ///
			placement(right) size(*0.8))	///
		text(-0.95 12.8 "{&bull} Low & Middle Income", color(maroon) 	///
			placement(right) size(*0.8) )	///
		text(-1.05 13 "{&bull} Years before 2015", color(gs8) 	///
			placement(right) size(*0.8))	///
		xsize(6) ysize(3)
graph export "../../draft/images/csr_20_av.pdf", replace
exit

/*
// now for Keynote and gavplot Keynote version

//		create confidence interval
	scalar t_a  = -invnormal((1-`c(level)'/100)/2)
	local ciopts "range(-58 30) lcolor(dkorange) lpattern(shortdash)"

twoway function y = b*x, range(-58 30) lcolor(midgreen) lpattern(dash) ///
	|| function y = (b-t_a*se_b)*x, `ciopts' ///
	|| function y = (b+t_a*se_b)*x, `ciopts' range(-48 30) ///
	|| scatter ey ex if ex<30 & ey<1 & ex>-58, msize(*0.1) mcolor(gs11) ///
	|| scatter ey2 ex2 if ex2<30 & ey2>-1.5, msize(*0.1) mcolor(gs11)	///
	|| scatter ey_hic ey_lmc ex if ex<30 & ex>-58, mcolor(`clr') msize(`ms')	///
	|| scatter ey_h3 ey_l3 ex if ex<30 & ex>-58, mcolor(`clr') msize(`ms') ///
		mlabcol(`clr') mlabel(`cc') mlabsize(`mls')	mlabvp(pos) ///
	||, /* title(Economic growth 1985-90 versus child survival) 	///
		subtitle(other years in gray)  */		///
		ylabel(, angle(0)) xlabel(-50(10)30)	///
		ytitle("e(GDP per worker growth, 1985-90 | X)") ///
		xtitle("e(Child survival rate 1950-70 | X)") legend(off) ///
		text(-1.25 10 " ", box placement(right) width(30) height(11) ///
			alignment(top) fcolor(none))	///
		text(-1.15 11 "{&bull} High Income", color(navy)  ///
			placement(right) size(*0.75))	///
		text(-1.25 10.8 "{&bull} Low & Middle Income", color(maroon) ///
			placement(right) size(*0.75) )	///
		text(-1.35 11 "{&bull} Years 1990+", color(gs11) 	///
			placement(right) size(*0.75))	///
		xsize(5) ysize(3) name(csr_20_kn)
graph export "../../draft/images/csr_20_kn.pdf", replace
graph combine csr_20_kn, xsize(5) ysize(3) ///
	title("Figure 2: Added-Variable Plot of Economic Growth Versus Child Survival")
graph export "../../../gavplot/csr_20_kn.pdf", replace
*/

//	now gavplot in black & white
	sort ex, stable
	scalar t_ase  = -invnormal((1-`c(level)'/100)/2)*se_b
	gen cih = (b+t_ase)*ex if ex>=-58 & ex<=30
	gen cil = (b-t_ase)*ex if ex>-58 & ex<30
	replace cih = -1.5 if cih < -1.5
	
twoway rarea cil cih ex if ex<30 & ex>-58, color(gs10) sort ///
	|| function y = b*x, range(-57 29) lcolor(black) ///
	|| scatter ey ex if ex<30 & ey<1 & ex>-58, msize(*0.5) msymb(oh) mcolor(gs4) ///
	|| scatter ey2 ex2 if ex2<30 & ey2>-1.5, msize(*0.5) msymb(oh) mcolor(gs4)	///
	||, ylabel(, angle(0)) xlabel(-50(10)30)	///
		ytitle("e(GDP per worker growth | X)") 	///
		xtitle("e(Child survival rate | X)") legend(off) ///
		xsize(5) ysize(3)
graph export "../../../gavplot/post/csr_20_bw.pdf", replace

// simple scatter & trend of csr_20 vs. gPWT5
twoway scatter gPWT5 csr_20 if gPWT5<20 & gPWT5>-20, 	///
		mcolor(maroon) msize(*0.5) msymb(oh) ///
	|| lfitci gPWT5 csr_20, lwidth(thick) 	///
		fcolor(midgreen%40) alcolor(gs10) alwidth(vthin)	///
		clcolor(forest_green) clwidth(medium) ///
	||, legend(off) ylabel(, angle(0)) xsize(16.8) ysize(10.5) ///
		ytitle("GDP per worker growth")			///
		xtitle("Child survival, previous generation")
graph export "../../../gavplot/csr_scat_kn.jpg", replace ///
	width(2560) height(1600) quality(100)
// now, black & white
twoway lfitci gPWT5 csr_20, lwidth(thick) 	///
		fcolor(gs10) alcolor(gs10) alwidth(vthin)	///
		clcolor(black) clwidth(medium) ///
	|| scatter gPWT5 csr_20 if gPWT5<20 & gPWT5>-20, 	///
		mcolor(black) msize(*0.5) msymb(oh) ///
	||, legend(off) ylabel(, angle(0)) xsize(5) ysize(3) ///
		ytitle("GDP per worker growth")			///
		xtitle("Child survival, previous generation")
graph export "../../../gavplot/post/csr_scat_bw.pdf", replace
exit

which xtavplot
set tracedepth 1
// set trace on
xtavplot csr_20, nocoef ///
	msize(*0.4) msymb(oh) ///
	ciplot(rarea) ciopts(fcolor(dkorange%20) 	///
		alcolor(gs10) alwidth(vthin))	///
	rlopts(clcolor(maroon) clwidth(medthick)) ///
	xlim(-58 30) ylim(-1.5 1)	/// 
	ylabel(-1(1)1, angle(0)) xlabel(-45(15)30)  ///
	title("Added-variable plot") ///
	ytitle("e(GDP per worker growth | X)")			///
	xtitle("e(Child survival, previous generation | X)")	///
	xsize(16.8) ysize(10.5)
graph export "../../../gavplot/csr_20_kn.jpg", replace ///
	width(2560) height(1600) quality(100)
exit

/*
gen ey_eap = ey if region=="EAP"
gen ey_eca = ey if region=="ECA"
gen ey_lac = ey if region=="LAC"
gen ey_mna = ey if region=="MNA"
gen ey_sas = ey if region=="SAS"
gen ey_ssa = ey if region=="SSA"
scatter ey ex if ex<30 & ey<1 & ex>-58, msize(*0.15) mcolor(gray) ///
	|| scatter ey2 ex2 if ex2<30 & ey2>-1.5, msize(*0.15) mcolor(gray)	///
	|| scatter ey_* ex if year==1985, ///
	mlabel(`cc') msize(`ms') mlabsize(`mls')
gen ey2_hic = ex2 if region=="HIC"
gen ey2_eap = ex2 if region=="EAP"
gen ey2_eca = ex2 if region=="ECA"
gen ey2_lac = ex2 if region=="LAC"
gen ey2_mna = ex2 if region=="MNA"
gen ey2_sas = ex2 if region=="SAS"
gen ey2_ssa = ex2 if region=="SSA"

scatter ey2_* ex2 if year==1985
scatter ey2_* ex2 if year==1990
scatter ey2_* ex2 if year==1995
scatter ey2_* ex2 if year==2000
scatter ey2_* ex2 if year==2005
scatter ey2_* ex2 if year==2010
scatter ey2_* ex2 if year==2015
*/
